package com.zjitc.controller;


import com.zjitc.beans.MessageBean;
import com.zjitc.pojo.Orders;
import com.zjitc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;
    private final String ORDER = "order";

    @RequestMapping("order")
    public ModelAndView findOrder(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if (!checkAdmin(session)) {
            modelAndView.setViewName("login");
        } else {
            modelAndView.setViewName("order/order");
            List<Orders> orders = orderService.findAllOrder();
            modelAndView.addObject("orders", orders);
        }
        return modelAndView;
    }

    @RequestMapping("findHistoryOrder")
    public ModelAndView findHistoryOrder(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if (!checkAdmin(session)) {
            modelAndView.setViewName("login");
        } else {
            modelAndView.setViewName("order/order");
            List<Orders> orders = orderService.findHistoryOrder();
            modelAndView.addObject("orders", orders);
        }
        return modelAndView;
    }

        @RequestMapping("delOrder")
    @ResponseBody
    public Object delOrder(String oid,String uid){
        orderService.admindeleteOrder(oid,uid);
        return new MessageBean(true,"删除成功");
    }

    @RequestMapping("tocheckOrder")
    public ModelAndView checkOrder(String oid,String uid, HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if (!checkAdmin(session)) {
            modelAndView.setViewName("login");
        } else {
            modelAndView.setViewName("order/checkOrder");
            Orders order = orderService.findOrderFromRedis(uid,oid);
            modelAndView.addObject("order",order);
        }
        return modelAndView;
    }

    @RequestMapping("shipments")
    @ResponseBody
    public Object shipments(String oid, String uid, HttpSession session) {
        if (!checkAdmin(session)) {
            return "-1";
        } else {
            boolean b = orderService.shipments(oid);
            if (b) {
                return "1";
            } else {
                return "0";
            }
        }
    }

}
